//
//  ZyaStoreQueryController.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2019/1/16.
//  Copyright © 2019 zwb. All rights reserved.
//

import UIKit

class ZyaStoreQueryController: UITableViewController {

    var startDate = today_pre_s(1080)
    var endDate = today_s()
    var key = ""
    var page = 1
    var dataArray = [ZyaStoreQueryModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.mj_header = KSRefreshHeader {
            self.page = 1
            self.fetchData()
        }
        tableView.mj_footer = KSRefreshFooter {
            self.page += 1
            self.fetchData()
        }
        tableView.mj_header!.beginRefreshing()
        tableView.tableFooterView = UIView()

        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .search, target: self, action: #selector(searchAction(_:)))
    }
    
    
    func fetchData() {
        
        KSNetwork.shared.doRequest(API.getStockMonth(begin_dt: startDate, end_dt: endDate, kf_id: USERINFO.usrid!, s_goods_code: key, curr_page: "\(page)", pageLength: "\(globalpagesize)"), showHud: false) { (data:Result4Store<StoreResult<ZyaStoreQueryModel>>) in
            if self.page == 1 {
                self.dataArray.removeAll()
            }
            let result = data.info
            let rows = result?.rows
            rows!.forEach {
                self.dataArray.append($0)
            }
            self.tableView.reloadData()
            self.tableView.mj_header!.endRefreshing()
            self.tableView.mj_footer!.endRefreshing()
            if rows!.count < globalpagesize {
                self.tableView.mj_footer!.endRefreshingWithNoMoreData()
            }
            
        }
 
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ZyaStoreQueryCell.identifier) as? ZyaStoreQueryCell else {
            fatalError("Could not create Cell")
        }
        cell.one = self.dataArray[indexPath.section]
        return cell
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        return dataArray.count
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 90
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 8
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }

    
    @objc fileprivate func searchAction(_ sender: Any) {
        let alert = KSAlertView()
        let txt0 = alert.addDatePicker(withLabel: "开始日期", action: nil)
        let txt1 = alert.addDatePicker(withLabel: "结束日期", action: nil)
        let txt2 = alert.addTextField("请输入商品名称", withLabel: "商品名称")
        
        txt0.text = self.startDate
        txt1.text = self.endDate
        _ = alert.addButton("查询") { [weak self] in
            self!.startDate = txt0.text!
            self!.endDate = txt1.text!
            self!.key = txt2.text!
           self!.tableView.mj_header!.beginRefreshing()
            
        }
        alert.show("查询条件", duration: 0.2, completeText: nil)
        
    }


}
